
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;

/**
 * @author laberinto
 * @version 1.0
 * @date 2024/5/22 11:09
 * @package PACKAGE_NAME
 * @message \\TODO
 * (⌓‿⌓)
 */
public class CheckHeaderCellRenderer  implements TableCellRenderer {
    TableModelProxy tableModel;
    JTableHeader tableHeader;
    final JCheckBox selectBox, selectBox1, selectBox2, selectBox3, selectBox4;

    public CheckHeaderCellRenderer(final JTable table) {
        this.tableModel = (TableModelProxy) table.getModel();
        this.tableHeader = table.getTableHeader();

        selectBox = new JCheckBox(tableModel.getColumnName(1));
        selectBox.setSelected(false);
        selectBox1 = new JCheckBox(tableModel.getColumnName(2));
        selectBox1.setSelected(false);
        selectBox2 = new JCheckBox(tableModel.getColumnName(3));
        selectBox2.setSelected(false);
        selectBox3 = new JCheckBox(tableModel.getColumnName(4));
        selectBox3.setSelected(false);
        selectBox4 = new JCheckBox(tableModel.getColumnName(5));
        selectBox4.setSelected(false);
        tableHeader.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() > 0) {
                    // 获得选中列
                    int selectColumn = tableHeader.columnAtPoint(e.getPoint());
                    switch (selectColumn) {
                        case 1:
                            boolean value = !selectBox.isSelected();
                            selectBox.setSelected(value);
                            tableModel.selectAllOrNull(value,selectColumn);
                            tableHeader.repaint();
                            break;
                        case 2:
                            boolean value1 = !selectBox1.isSelected();
                            selectBox1.setSelected(value1);
                            tableModel.selectAllOrNull(value1,selectColumn);
                            tableHeader.repaint();
                            break;
                        case 3:
                            boolean value2 = !selectBox2.isSelected();
                            selectBox2.setSelected(value2);
                            tableModel.selectAllOrNull(value2,selectColumn);
                            tableHeader.repaint();
                            break;
                        case 4:
                            boolean value3 = !selectBox3.isSelected();
                            selectBox3.setSelected(value3);
                            tableModel.selectAllOrNull(value3,selectColumn);
                            tableHeader.repaint();
                            break;
                        case 5:
                            boolean value4 = !selectBox4.isSelected();
                            selectBox4.setSelected(value4);
                            tableModel.selectAllOrNull(value4,selectColumn);
                            tableHeader.repaint();
                            break;
                        default:
                            break;

                    }

                }
            }
        });
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
                                                   int row, int column) {

        // TODO Auto-generated method stub
        String valueStr = (String) value;
        JLabel label = new JLabel(valueStr);
        label.setHorizontalAlignment(SwingConstants.CENTER); // 表头标签剧中
        selectBox.setHorizontalAlignment(SwingConstants.CENTER);// 表头标签剧中
        selectBox.setBorderPainted(true);
        selectBox1.setHorizontalAlignment(SwingConstants.CENTER);// 表头标签剧中
        selectBox1.setBorderPainted(true);
        selectBox2.setHorizontalAlignment(SwingConstants.CENTER);// 表头标签剧中
        selectBox2.setBorderPainted(true);
        selectBox3.setHorizontalAlignment(SwingConstants.CENTER);// 表头标签剧中
        selectBox3.setBorderPainted(true);
        selectBox4.setHorizontalAlignment(SwingConstants.CENTER);// 表头标签剧中
        selectBox4.setBorderPainted(true);
        JComponent component;
        switch (column) {
            case 1:
                component = selectBox;
                break;
            case 2:
                component = selectBox1;
                break;
            case 3:
                component = selectBox2;
                break;
            case 4:
                component = selectBox3;
                break;
            case 5:
                component = selectBox4;
                break;
            default:
                component = label;

                break;
        }


        component.setForeground(tableHeader.getForeground());
        component.setBackground(tableHeader.getBackground());
        component.setFont(tableHeader.getFont());
        component.setBorder(UIManager.getBorder("TableHeader.cellBorder"));

        return component;
    }


}
